package com.example.zhangpeng.socketclient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String SUCCED="succed";
    private LinearLayout activityMain;
    private EditText etIp;
    private Button btnConnect;
    private TextView tvShow;
    private Button btnSend;
    private EditText etMessageSend;

    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;
    private Socket socket;
    private String ip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }
    
    private void initView(){
        activityMain = (LinearLayout) findViewById(R.id.activity_main);
        etIp = (EditText) findViewById(R.id.et_ip);
        btnConnect = (Button) findViewById(R.id.btn_connect);
        tvShow = (TextView) findViewById(R.id.tv_show);
        btnSend = (Button) findViewById(R.id.btn_send);
        etMessageSend = (EditText) findViewById(R.id.et_message_send);


    }
    private void initListener() {
        btnConnect.setOnClickListener(this);
        btnSend.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_connect :
                ip=etIp.getText().toString().trim();
                connect();
                break;
            case R.id.btn_send:
                sendMessage();
                break;
        }
    }


    private void connect() {
        new MyAsyncTask().execute();
    }
    private void sendMessage() {
       tvShow.append("我说："+etMessageSend.getText().toString()+"\n");
        try {
            bufferedWriter.write(etMessageSend.getText().toString()+"\n");
            //强制输出
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class MyAsyncTask extends AsyncTask<Void,String ,Void>{

        @Override
        protected Void doInBackground(Void... params) {
            //1.创建 socket 链接
            try {
                socket=new Socket("10.0.2.2",12345);
                bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                publishProgress(SUCCED);
            } catch (IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,"无法建立链接",Toast.LENGTH_SHORT).show();
                    }
                });

                e.printStackTrace();
            }
            // 读取服务端 socket 发来的消息
            String line=null;
            if(bufferedReader==null){
                return null;
            }
            try {
                while ((line=bufferedReader.readLine())!=null){
                    publishProgress(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            if(values[0].equals(SUCCED)){
                Toast.makeText(MainActivity.this,"建立链接成功",Toast.LENGTH_SHORT).show();
            }
            //显示服务端发来的消息
            tvShow.append("别人说:"+values[0]+"\n");
            super.onProgressUpdate(values);
        }
    }
}
